package own.stu.jobgib.playown.alg.leetcode.editor.cn.demo;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Station {

    private String name;

    private int nums;

    private Lock lock = new ReentrantLock();

    private Condition takeCondition = lock.newCondition();

    public Station(String name, int nums) {
        this.name = name;
        this.nums = nums;
    }

    public int getNums() {
        return nums;
    }

    public void returnCar(int nums) {
        lock.lock();
        try{
            this.nums += nums;
            takeCondition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public void takeCar(int nums){
        lock.lock();
        try {
            while (this.nums < nums) {
                takeCondition.await();
            }
            this.nums -= nums;
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }
}
